package com.xpec.c4.service.common;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ms.player.Player;
import com.ms.service.module.zone.BasicZone;
import com.ms.service.module.zone.module.BasicZoneModule;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.service.annotations.In;
import com.xpec.c4.service.contexts.Contexts;
import com.xpec.c4.service.module.BasicServiceModule;
import com.xpec.c4.service.module.common.PlayerCommonInfoManager;


public class BaseMessageAction {
	
	private static Log log = LogFactory.getLog(BaseMessageAction.class);
	
	
	/**
	 * 角色ID
	 */
	public GUID playerID;

	/**
	 * sessionID<Br>
	 * 只有client发送的，才会有此值
	 */
	public GUID sessionID;
	
	/**
	 * 接受的模块ID
	 */
	public String moduleID;
	
	/**
	 * 发送的server<br>
	 * 只有server之间的发送，才会有此值
	 */
	@In
	public String srcService;
	
	/**
	 * 帐号ID
	 */	
	@In
	public GUID accountID;

	/**
	 * 取得發出消息的 client player 資料
	 * 
	 * @return player data
	 */
	public Player getPlayer() {
		if(sessionID == null){ //sessionId null
			if(log.isErrorEnabled()) {
				log.error("[BaseMessageAction]getPlayer sessionId == null");
			}
		}
		
		GUID playerGuid = PlayerCommonInfoManager.getInstance().getPlayerGuidBySession(sessionID);
		if(playerGuid == null){ //playerId null
			if(log.isErrorEnabled()) {
				log.error("[BaseMessageAction]getPlayer playerGuid == null");
			}
		}
	
		BasicServiceModule basicServiceModule = Contexts.getModule();
		if (basicServiceModule instanceof BasicZoneModule) {
			BasicZoneModule basicZoneModule = (BasicZoneModule) basicServiceModule;
			BasicZone basicZone = basicZoneModule.getZone();
			Player player = basicZone.getPlayer(playerGuid);
			if (player == null) { // player null
				if (log.isErrorEnabled()) {
					log.error("[BaseMessageAction]getPlayer player == null");
				}
			}
			return player;
		}
		
		if (log.isErrorEnabled()) { // basicZoneModule null
			log.error("[BaseMessageAction]getPlayer basicZoneModule not instanceof BasicZoneModule");
		}

		return null;
	}

	/**
	 * 取得接收消息的 module instance
	 * 
	 * @return module instance
	 */
	public BasicServiceModule getModule() {
		return Contexts.getModule();
	}

	public GUID getPlayerID() {
		return playerID;
	}

	public void setPlayerID(GUID playerID) {
		this.playerID = playerID;
	}

	public GUID getSessionID() {
		return sessionID;
	}

	public void setSessionID(GUID sessionID) {
		this.sessionID = sessionID;
	}

	public String getModuleID() {
		return moduleID;
	}

	public void setModuleID(String moduleID) {
		this.moduleID = moduleID;
	}

	public String getSrcService() {
		return srcService;
	}

	public void setSrcService(String srcService) {
		this.srcService = srcService;
	}

	public GUID getAccountID() {
		return accountID;
	}

	public void setAccountID(GUID accountID) {
		this.accountID = accountID;
	}

//	/**
//	 * 通过playerID获得Player
//	 * @return
//	 */
//	public Player getPlayer(){
//		return getPlayer(playerID);
//	}
//
//	/**
//	 * 通过playerID获得Player
//	 * @return
//	 */
//	public static Player getPlayer(GUID playerID){
//		if(Contexts.getModule() instanceof BasicZoneModule){
//			return ((BasicZoneModule)Contexts.getModule()).getPlayerDataAgent().getPlayerByGuid(playerID);
//		}
//		//沒進入到zone 的action目前不支援此種方式
//		else if(Contexts.getModule() instanceof LoginModule){
//			return ((LoginModule)Contexts.getModule()).getPlayerDataAgent().getPlayerByGuid(playerID);
//		}
//		else 
//			return null;
//	}
}
